//@version: $Id: Shape.cpp 144 2010-12-09 00:39:17Z bills.jared $

#include "Shape.h"

Shape::Shape()
{
	//init();
}

void Shape::init()
{
	//initialize shape type
	seed(SHAPE_SIZE);
	shapeType = generate();
	
	//gridX = gridY = gridZ = 0;
	/*	Create object
	**	Load .X file
	**	Scale and position object in 3-space 
	**	Save object in AllObjects list
	*/
	D3DXVECTOR3 cvPos( 0.0f, 0.0f, 0.0f);

	/* Mesh file names for the different objects */
	const char* cszMeshFileName[] =
	{
		"Meshes/OVAL.x",
		"Meshes/sphere.x",
		"Meshes/STAR_HEX.x",
		"Meshes/SMALL_SQUARE.x",
		"Meshes/TEARDROP.x",
		"Meshes/JEWEL.x"
	};

	/* Create objects and save pointers in pObject array */
	for(int i = 0; i < SHAPE_SIZE; i++)
	{
		/* Create object i, not spinning to start with */
		pObject[i] = new SpinningObject(cvPos);
		assert(pObject[i] != NULL);
		pObject[i]->SetSpinning(false);

		/* Load associated mesh file plus all materials and textures therein */
		Renderer::Instance()->LoadMeshFile(pObject[i], cszMeshFileName[i]);
		assert(pObject[i]->GetMesh() != NULL);

		/* Scale and position new object. Use size and center of bounding sphere
		** to ensure we can see the mesh even if very large or very small in its
		** original .X file. Also, use bounding sphere center to correct
		** position to origin
		*/
		const float cfRadius = pObject[i]->GetBoundingRadius();
		if(cfRadius > 0.0f)
		{
			pObject[i]->SetScale(0.9f/cfRadius);	// size == 0.9
		}
	}
}

// the draw function will draw what ever 
// is in grid space you pass. r = row number c = column number
// just waiting on location so i can translate array index to 
// screen coords
SpinningObject* Shape::draw()
{
	//D3DXMatrixTranslation(&matTrans, gridX, gridY, gridZ);

	/* Relies on the order of the shape enum matching its shapeType */
	SpinningObject * currentObj = pObject[shapeType];
	currentObj->SetPosition(D3DXVECTOR3(m_trans._41,m_trans._42,m_trans._43));
	return currentObj;
}

/*************************************************************************
*location
*creates and returns a matrix based off of array indices
*arguments: indexX / indexY - array index values
*return value: matLoc - matrix to be translated and drawn to screen
**************************************************************************/
D3DXMATRIX Shape::location(int a_indexX, int a_indexY)
{
	float matrixX, matrixY;					//x and y values to be translated
	D3DXMATRIX matX, matY, matLoc;			//matrices for X,Y and return location

	//the amount of space that must exist so adjacent shapes don't overlap
	const float SPACE_BETWEEN_SHAPES = (float)1.8;
	/*
	* since there are an even number of rows and columns (6),
	* no shape will be directly in the middle of the map (3 on each side).
	* so, the "middle" shapes must be half of SPACE_BETWEEN_SHAPES distance away from 0,
	* so that they are exactly SPACE_BETWEEN_SHAPES distance away from each other
	*/
	const float UNITS_TO_MAP_OUTER_LIMIT_WIDTH = (WIDTH / 2) - (float).5;
	const float UNITS_TO_MAP_OUTER_LIMIT_HEIGHT = (HEIGHT / 2) - (float).5;

	//convert array index X value into translation-dependent value
	matrixX = 0 - (SPACE_BETWEEN_SHAPES * UNITS_TO_MAP_OUTER_LIMIT_WIDTH) + (SPACE_BETWEEN_SHAPES * a_indexX);
	/*switch(a_indexX)
	{
		case 0:	matrixX = (float)-2;	break;
		case 1:	matrixX = (float)-1.2;	break;
		case 2:	matrixX = (float)-0.4;	break;
		case 3:	matrixX = (float)0.4;	break;
		case 4:	matrixX = (float)1.2;	break;
		case 5:	matrixX = (float)2;		break;
	}*/
	//convert array index Y value into translation-dependent value
	matrixY = (SPACE_BETWEEN_SHAPES * UNITS_TO_MAP_OUTER_LIMIT_HEIGHT) - (SPACE_BETWEEN_SHAPES * a_indexY);
	/*switch(a_indexY)
	{
		case 0:	matrixY = (float)-2;	break;
		case 1:	matrixY = (float)-1.2;	break;
		case 2:	matrixY = (float)-0.4;	break;
		case 3:	matrixY = (float)0.4;	break;
		case 4:	matrixY = (float)1.2;	break;
		case 5:	matrixY = (float)2;		break;
	}*/

	//translate X and Y values based off converted values
	D3DXMatrixTranslation(&matX, matrixX,0.0,0.0);
	D3DXMatrixTranslation(&matY, 0.0,matrixY,0.0);

	//create original matrix identity and multiply X and Y values
	D3DXMatrixIdentity(&matLoc);
	D3DXMatrixMultiply(&matLoc, &matLoc, &matX);
	D3DXMatrixMultiply(&matLoc, &matLoc, &matY);

	//return multiplied and formatted matrix
	return matLoc;
}
void Shape::setSpin(bool k)
{
	pObject[shapeType]->SetSpinning(k);
	/*
	switch(shapeType) {
	case PYRAMID:
		pObject[2]->SetSpinning(k);
		break;
	case CUBE:
		pObject[1]->SetSpinning(k);
		break;
	case SPHERE:
		pObject[0]->SetSpinning(k);
		break;
	case STAR:
		pObject[5]->SetSpinning(k);
		break;
	case HEXAGON:
		pObject[3]->SetSpinning(k);
		break;
	case DIAMOND:
		pObject[4]->SetSpinning(k);
		break;
	}
	*/
	/*
	switch(shapeType)
	{
	case 1:
			pObject1->SetSpinning(k);
			break;
	case 2:
		pObject2->SetSpinning(k);
		break;
	case 3:
		pObject3->SetSpinning(k);
		break;
	case 4:
		pObject4->SetSpinning(k);
		break;
	case 5:
		pObject5->SetSpinning(k);
		break;
	case 6:
		pObject6->SetSpinning(k);
		break;
	}
	*/
}